package com.briup.bms.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.briup.bms.bean.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import com.briup.bms.bean.Region;
import com.briup.bms.common.utils.Page;
import com.briup.bms.common.utils.Result;
import com.briup.bms.service.IRegionService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "区域信息服务")
@RestController
@RequestMapping("/region")
public class RegionController {
	
	@Autowired
	IRegionService regionService;
	
	@ApiOperation(value = "查询所有区域信息")
	@GetMapping("/all")
	public Result<List<Region>> findAll(){
		
		return Result.success(regionService.findAll());
	}

	
	@ApiOperation(value = "按照条件查找区域信息，并分页显示（基于PageHelper）",
			notes = "1.条件只有名称。<br/>"
			+ "2.如果没有指定页码，默认显示第一页<br/>"
			+ "3.如果没有指定页面大小，默认每页显示10条")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "区域名称"),
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1",dataType = "int"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的数据个数",
						 defaultValue = "10",allowableValues = "5,10,15,20",dataType = "int")
	})
	@GetMapping(value = "/by/pageHelper")
	public Result<PageInfo<Region>> findByPageCondition(
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String name) {
		PageInfo<Region> page = regionService.findByPageHelper(pageNum,pageSize,name);
		return Result.success(page);
	}
	
	
	
	@ApiOperation(value = "按照条件查找区域信息，并分页显示",notes = "1.条件只有名称。<br/>"
			+ "2.如果没有指定页码，默认显示第一页<br/>"
			+ "3.如果没有指定页面大小，默认每页显示10条")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "name", value = "区域名称"),
		@ApiImplicitParam(name = "pageNum",value = "页码",defaultValue = "1",dataType = "int"),
		@ApiImplicitParam(name = "pageSize",value = "每页显示的数据个数",
						 defaultValue = "10",allowableValues = "5,10,15,20",dataType = "int")
	})
	@GetMapping("/by/condition")
	public Result<Page<Region>> findByCondition(
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10")int pageSize,
			String name) {
		Page<Region> page = regionService.findByPageAndCondition(pageNum, pageSize, name);
		return Result.success(page);
	}
	
//	public Result<List<Region>> findByCondition(
//			@RequestParam(defaultValue = "1") int pageNum,
//			@RequestParam(defaultValue = "10")int pageSize,
//			String name) {
//		List<Region> list = regionService.findByCondition(pageNum,pageSize,name);
//		return Result.success(list);
//	}
	
	
	@ApiOperation(value = "批量删除")
	@DeleteMapping("/batch")
	public Result<String> deleteByBatch(@RequestParam(name = "ids") Set<Long> ids) {
		regionService.deleteByBatch(ids);
		return Result.success("批量删除成功！");
	}
	
	@ApiOperation(value = "按照id进行删除",notes = "id当做路径参数传递")
	@ApiImplicitParam(name = "id",value = "区域编号",dataType = "long")
	@DeleteMapping("/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		regionService.deleteById(id);
		return Result.success("删除【"+id+"】号区域成功！");
	}

	@ApiOperation(value = "保存区域信息",notes = "保存时区域编号自动生成")
	@PostMapping
	public Result<String> saveRegion(@RequestBody Region region) {
		regionService.save(region);
		return Result.success("保存成功！");
	}
	
	@ApiOperation(value = "更新区域信息",notes = "更新时一定携带区域编号")
	@PutMapping
	public Result<String> updateRegion(@RequestBody Region region) {
		regionService.modify(region);
		return Result.success("更新["+region.getId()+"]号区域成功！");
	}
	
	@ApiOperation(value = "保存或更新区域信息",notes = "1. 如果没有传递区域id，那么保存区域信息<br/>"
			+ "2. 如果传递区域id，那么执行区域信息的更新")
	@PostMapping("/region")
	public Result<String> saveOrUpdate(@RequestBody Region region) {
		regionService.saveOrUpdate(region);
		return Result.success(region.getId()==null?"保存成功！":"更新成功！");
	}
}
